function generateGraphic(){
	$(".generate").click(function(event){
		var elemImage = $("#image");
		var elemProject = $("#projectName");
		var elemRepositories = $("#repositories");
		var elemGraphics = $("#graphics");
		var elemMetrics = $("#metrics");
		var elemTypes = $("#types");
		var elemStatus = $("#status");
		var elemStart = $("#start");
		var elemFinish = $("#finish");
		var elemPercent = $("#percent");
		var elemOrder = $("#orderByName");
		var elemTime = $("#time");
		var elemDeveloper = $("#developer");
		
		var validGraphics = validateValue(elemGraphics, "Graphic");
		var validMetrics = validateValue(elemMetrics, "Metric");
		var validTypes = validateValue(elemTypes, "Type");
		var validStatus = validateValue(elemStatus, "Status");
		var validPercent = validateNumber(elemPercent, "Percent");
		
		if (validGraphics && validMetrics && validTypes && validStatus && validPercent){
			elemImage.attr("src", "../images/spinner.gif");
			elemImage.show();
			elemImage.attr("src", "/devmeter/report/graphic.action?project.name="+ elemProject.val() +"&graphic="+ elemGraphics.val() +"&metric="+ elemMetrics.val() +"&type="+ elemTypes.val() +"&status="+ elemStatus.val() +"&start="+ elemStart.val() +"&finish="+ elemFinish.val() +"&percent="+ elemPercent.val() +"&orderByName="+ elemOrder.attr("checked") +"&time="+ elemTime.val() +"&nameDeveloper="+ elemDeveloper.val());
			showAdditionalOptions(elemGraphics.val());
			Statistics.getStatistics(elemProject.val(), elemMetrics.val(), elemTypes.val(), elemStatus.val(), elemStart.val(), elemFinish.val(), updateStatistics);
		}
		
	});
}

function validateValue(elem, name){
	if (elem == null || elem.val() == null || elem.val() == '' || elem.val() == '--Select an Option--'){
		alert("The value in the field " + name + " can't be null");
		return false;
	}
	return true;
}

function validateNumber(elem, name){
	var expression = /^-{0,1}\d*\.{0,1}\d+$/;
	if (elem != null && elem.val() != null && elem.val() != '' && !expression.test(elem.val())){
    	alert("The value in the field " + name + " doesn't a number");
    	return false;
	}
	return true;
}

function showAdditionalOptions(graphic) {
	var elemPanelOptions = $("#panelOptions");
	elemPanelOptions.show();

	var elemChangeTime = $("#changeTime");
	if (graphic == 'LINE'){
		elemChangeTime.show();
	} else {
		elemChangeTime.hide();
	}
}

function updateStatistics(data) {
	var elemDiv = $("#statistics");
	elemDiv.empty();
	
	elemDiv.append("<p>Statistics Data about Contribution of Developers</p>");
	elemDiv.append("<table />");
	for ( var i = 0; i < data.length; i++) {
		var array = data[i].split("%%");
		var colData = "<td>"+ array[0] +": </td>";
		var colValue = "<td>"+ array[1] +"</td>";
		elemDiv.append("<tr>" + colData + colValue + "</tr>");
	}

	var elemPanelStatistics = $("#panelStatistics");
	elemPanelStatistics.show();
}